﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library.Components.Security;
using NCS.Library.Utilities;
using NCS.Module.LinkSite.Library.DataAccess;
using NCS.Module.LinkSite.Library.Data;
using NCS.Library;

namespace NCS.Module.LinkSite.Admin
{
    public partial class CatalogManager : AuthenticatedControl
    {
        private int totalRecord = 0;
        protected int iRecord = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                show_Title();
            }
        }
        protected void gvTieude_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                try
                {
                    LinkCatalogDB.Delete(Convert.ToInt32(e.CommandArgument));
                    MessageBox.Show(MiscUtility.MSG_UPDATE_SUCCESS);
                    Response.Redirect(Request.RawUrl);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            if (e.CommandName == "EditAnything")
            {
                try
                {
                    LinkCatalog qinfo = LinkCatalogDB.GetInfo(Convert.ToInt32(e.CommandArgument));
                    txtIDTitle.Text = qinfo.IDTitle.ToString();
                    txtTieuchi.Text = qinfo.TitleName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }
        protected void gvTieude_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTieude.PageIndex = e.NewPageIndex;
            show_Title();
        }

        protected void gvTieude_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dv = (DataRowView)e.Row.DataItem;
                e.Row.Cells[1].Text = Convert.ToString(e.Row.DataItemIndex + 1);
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                btnDel.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
            }
        }
        protected void btnCapnhat_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtIDTitle.Text.Trim()))
                {
                    MessageBox.Show("Hãy chọn 1 mục cần chỉnh sửa!");
                    return;
                }
                if (string.IsNullOrEmpty(txtTieuchi.Text.Trim()))
                {
                    MessageBox.Show("Hãy nhập tên mục!");
                    txtTieuchi.Focus();
                    return;
                }
                LinkCatalog info = LinkCatalogDB.GetInfo(Convert.ToInt32(txtIDTitle.Text));
                info.TitleName =  txtTieuchi.Text;
                info.TitleLang = AppEnv.GetLanguage();
                LinkCatalogDB.Update(info);
                MessageBox.Show(MiscUtility.MSG_UPDATE_SUCCESS);
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void show_Title()
        {
            DataTable dt = LinkCatalogDB.GetAll();
            if (dt != null) totalRecord = dt.Rows.Count;
            Style_Gridview(gvTieude, Convert.ToInt32(dt.Rows.Count.ToString()));
            gvTieude.DataSource = dt;
            gvTieude.RowDataBound += new GridViewRowEventHandler(gvTieude_RowDataBound);
            gvTieude.DataBind();
        }

        void Style_Gridview(GridView grv, int _TotalRecord)
        {
            grv.PagerSettings.Position = PagerPosition.Bottom; // vi tri duoi
            grv.PagerSettings.PageButtonCount = 3; // hien thi 2 so
            iRecord = 7;
            grv.PageSize = iRecord;

            if (_TotalRecord > iRecord * 2)
            {
                grv.AllowPaging = true;
            }
            else
            {
                grv.AllowPaging = false;
            }
        }
        protected void txtThemmoi_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtTieuchi.Text.Trim()))
                {
                    MessageBox.Show("Hãy nhập tên mục!");
                    txtTieuchi.Focus();
                    return;
                }
                LinkCatalog info = new LinkCatalog();
                info.TitleName = txtTieuchi.Text;
                info.TitleLang = AppEnv.GetLanguage();
                LinkCatalogDB.Insert(info);
                MessageBox.Show(MiscUtility.MSG_UPDATE_SUCCESS);
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}